﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;

namespace DietPlanning.Models
{

    public class UserDietModels
    {
        public User User { get; set; }
        public List<Diet> Diet { get; set; }
        public List<DietDetail> DietDetail { get; set; }
        public int ViewDays { get; set; }
        //public void GetCalo()
        //{
        //    int age = 0;
        //    if (User.Birthday != null)
        //    {
        //        age = DateTime.Now.Year - User.Birthday.Value.Year;
        //    }
        //    if (User.Male == 0)
        //    {
        //        var calory = User.Weight * 9.99 + User.Height * 6.25 - age * 4.92 + 5;
        //        if (calory != null) Diet.Single(o => o.UserID == User.UserID && o.isNew == 1).CalorieNeed = (int)calory;
        //    }
        //    else
        //    {
        //        var calory = User.Weight * 9.99 + User.Height * 6.25 - age * 4.92 - 161;
        //        if (calory != null) Diet.Single(o => o.UserID == User.UserID && o.isNew == 1).CalorieNeed = (int)calory;
        //    }
        //}
        public void GetInfo()
        {
            using (var db = new DailyFoodEntities())
            {
                List<Diet> diet = db.Diets.Where(o => o.UserID == User.UserID).ToList();
                List<DietDetail> dietDetail = new List<DietDetail>();
                foreach (Diet item in diet)
                {
                    dietDetail.AddRange(db.DietDetails.Where(o => o.DietID == item.DietID));
                }
                this.Diet = diet;
                this.DietDetail = dietDetail;
            }
        }

        public void CreateDietDefault()
        {
            using (var db = new DailyFoodEntities())
            {
                Diet diet = new Diet();
                diet.DietName = "Default";
                diet.isNew = 1;
                diet.DateTimeBegin = DateTime.Now;
                diet.DateTimeEnd = DateTime.Now.Add(TimeSpan.FromDays(30));
                diet.UserID = User.UserID;
                //Calculator Calories
                int age = 0;
                if (User.Birthday != null)
                {
                    age = DateTime.Now.Year - User.Birthday.Value.Year;
                }
                if (User.Male == 0)
                {
                    var calory = User.Weight * 9.99 + User.Height * 6.25 - age * 4.92 + 5;
                    if (calory != null) diet.CalorieNeed = (int)calory;
                }
                else
                {
                    var calory = User.Weight * 9.99 + User.Height * 6.25 - age * 4.92 - 161;
                    if (calory != null) diet.CalorieNeed = (int)calory;
                }
                db.Diets.AddObject(diet);
                db.SaveChanges();
                // var x = db.Diets.First(o => o.DietName == diet.DietName);
                CreateDietDetail(diet);
            }
        }

        public int CreateDietCustom(Diet diet)
        {
            using (var db = new DailyFoodEntities())
            {
                db.Diets.AddObject(diet);
                db.SaveChanges();
                CreateDietDetail(diet);
                return diet.DietID;
            }

        }
        public void CreateDietDetail(Diet diet)
        {

            using (var db = new DailyFoodEntities())
            {
                //Create for 7 day
                int i = 0;
                var timeSpan = diet.DateTimeEnd - diet.DateTimeBegin;
                while (timeSpan != null && i < timeSpan.Value.Days)
                {
                    if (diet.CalorieNeed != null)
                    {
                        foreach (Dish dish in RandomFood(diet.CalorieNeed))
                        {
                            DietDetail dietDetail = new DietDetail();
                            dietDetail.DietID = diet.DietID;
                            dietDetail.DishID = dish.DishID;
                            dietDetail.Time = dish.Time;
                            DateTime? dt = diet.DateTimeBegin + TimeSpan.FromDays(i);
                            dietDetail.DateTime = dt;
                            db.DietDetails.AddObject(dietDetail);
                            db.SaveChanges();
                        }
                    }
                    i++;
                }

            }
        }

        //Choose dish
        public List<Dish> RandomFood(int? totalCalories)
        {
            List<Dish> list = new List<Dish>();
            int num = 0;
            using (var db = new DailyFoodEntities())
            {
                int calories = 0;
                Random r = new Random();

                while (calories < totalCalories)
                {
                    //Add morning
                    if (num < 2)
                    {
                        var listDish = db.Dishes.Where(o => o.Time.Contains("morning")).ToList();

                        int index = r.Next(0, listDish.Count - 1);
                        var dishchoosen = listDish;
                        dishchoosen[index].Time = "morning";
                        list.Add(dishchoosen[index]);
                        if (dishchoosen[index].Calories != null)
                            calories += (int)dishchoosen[index].Calories;
                    }
                    //Add Noon
                    else if (num >= 2 && num <= 4)
                    {
                        var listDish = db.Dishes.Where(o => o.Time.Contains("noon")).ToList();

                        int index = r.Next(0, listDish.Count - 1);
                        var dishchoosen = listDish;
                        dishchoosen[index].Time = "noon";
                        list.Add(dishchoosen[index]);
                        if (dishchoosen[index].Calories != null)
                            calories += (int)dishchoosen[index].Calories;

                    }
                    //Add evening
                    else
                    {
                        var listDish = db.Dishes.Where(o => o.Time.Contains("evening")).ToList();

                        int index = r.Next(0, listDish.Count - 1);
                        var dishchoosen = listDish;
                        dishchoosen[index].Time = "evening";
                        list.Add(dishchoosen[index]);
                        if (dishchoosen[index].Calories != null)
                            calories += (int)dishchoosen[index].Calories;
                    }
                    num++;
                }

            }
            return list;
        }

        public void ChangeNewToOld()
        {
            using (var db = new DailyFoodEntities())
            {//DailyFoodEntities
                foreach (var diet1 in Diet)
                {
                    //Change New to Old
                    if (diet1.isNew == 1 && diet1.UserID == User.UserID)
                    {
                        db.Diets.First(o => o.DietID == diet1.DietID).isNew = 0;
                        db.SaveChanges();
                    }
                }
            }

        }
    }


}
